import { createRouter, createWebHashHistory } from 'vue-router'

// 动态导入各页面组件（保持原导入）
const HomeView = () => import('@/views/HomeView.vue')
const PersonalCenter = () => import('@/views/PersonalCenter/index.vue')
const EditPassword = () => import('@/views/PersonalCenter/EditPassword.vue')
const PersonalInfo = () => import('@/views/PersonalCenter/PersonalInfo.vue')
const AdminManage = () => import('@/views/AdminManage/index.vue')
const AdminManagePage = () => import('@/views/AdminManage/AdminManagePage.vue')
const BasicData = () => import('@/views/BasicData/index.vue')
const GoodsTypeManage = () => import('@/views/BasicData/GoodsTypeManage.vue') 
const AdministratorAudit = () => import('@/views/BasicData/AdministratorAudit.vue') 
const LostManage = () => import('@/views/LostManage/index.vue')
const LostInfoPage = () => import('@/views/LostManage/LostInfoPage.vue')
const SeekManage = () => import('@/views/SeekManage/index.vue')
const SeekInfoPage = () => import('@/views/SeekManage/SeekInfoPage.vue')
const UserManage = () => import('@/views/UserManage/index.vue')
const UserInformation = () => import('@/views/UserManage/UserInformation.vue')
const Login = () => import('@/views/Login.vue') 
// 关键：导入 Layout 组件，确保路由和布局联动
import Layout from '@/components/Layout.vue' 

const routes = [
  {
    path: '/',
    redirect: '/login' 
  },
  // 登录页（无需权限）
  {
    path: '/login',
    component: Login,
    meta: { title: '登录' }
  },
  // 主布局路由（所有需要登录的页面都放在这里）
  {
    path: '/',
    component: Layout, // 登录后统一通过 Layout 渲染
    meta: { requiresAuth: true }, // 布局本身需要登录
    children: [
      {
        path: 'home',
        component: HomeView,
        meta: { title: '首页' }
      },
      {
        path: 'personal-center',
        component: PersonalCenter,
        meta: { title: '个人中心' },
        children: [
          {
            path: 'edit-password',
            component: EditPassword,
            meta: { title: '修改密码' }
          },
          {
            path: 'personal-info',
            component: PersonalInfo,
            meta: { title: '个人信息' }
          }
        ]
      },
      {
        path: 'admin-manage',
        component: AdminManage,
        meta: { title: '管理员管理' },
        children: [
          {
            path: 'new-admin-page', 
            component: AdminManagePage,
            meta: { title: '管理员信息' }
          }
        ]
      },
      {
        path: 'basic-data',
        component: BasicData,
        meta: { title: '基础数据管理' },
        children: [
          {
            path: 'goods-type-manage',
            component: GoodsTypeManage,
            meta: { title: '物品类型管理' }
          },
          {
            path: 'administrator-audit',
            component: AdministratorAudit,
            meta: { title: '审核拾取-遗失' }
          }
        ]
      },
      {
        path: 'lost-manage',
        component: LostManage,
        meta: { title: '失物信息管理' },
        children: [
          {
            path: 'lost-info',
            component: LostInfoPage,
            meta: { title: '失物信息详情' }
          }
        ]
      },
      {
        path: 'seek-manage',
        component: SeekManage,
        meta: { title: '寻物启事管理' },
        children: [
          {
            path: 'seek-info',
            component: SeekInfoPage,
            meta: { title: '寻物启事详情' }
          }
        ]
      },
      {
        path: 'user-manage',
        component: UserManage,
        meta: { title: '用户管理' },
        children: [
          {
            path: 'user-info',
            component: UserInformation,
            meta: { title: '用户信息' }
          }
        ]
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫：权限验证核心逻辑
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }

  // 判断是否需要登录
  const requiresAuth = to.meta.requiresAuth
  // 从 localStorage 获取登录状态
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'

  if (requiresAuth) {
    if (isLoggedIn) {
      // 已登录，继续跳转
      next()
    } else {
      // 未登录，跳转到登录页，并记录当前路径
      next({ 
        path: '/login', 
        query: { redirect: to.fullPath } 
      })
    }
  } else {
    // 无需登录的页面（如登录页），直接放行
    next()
  }
})

export default router